package se.webbzon.boltzmann.menu;

import java.awt.image.ImageObserver;
import java.util.Arrays;
import java.util.List;

import se.webbzon.boltzmann.configuration.Controls;
import se.webbzon.boltzmann.input.Button;
import se.webbzon.boltzmann.input.Input;
import se.webbzon.boltzmann.input.InputListener;

public class ControlEditPage extends MFlowPage {
	
	// The input to edit
	private final Input input;
	
	// The input listener
	private final InputListener listener;
	
	// The overlay on the heads-up display when a button is edited
	private final MOverlay overlay;
	
	// The current editable button
	private EditButton editableButton;

	/*============================================================
	Constructors
	============================================================*/
	
	public ControlEditPage(ImageObserver observer, AbstractGameMenuLayout layout, MPage parent) {
		super(observer,layout.getPageLayout(), parent,"Edit");
		input = Controls.instance().getInput();
		listener = new InputListenerImpl();
		List<Button> list = Arrays.asList(Button.values());
		if (input.hasPrimaryAxis()) {
			list.remove(Button.LEFT);
			list.remove(Button.RIGHT);
			list.remove(Button.UP);
			list.remove(Button.DOWN);
		}
		
		addItem(new PageLabel(observer, layout.getLabelLayout(),this));
		addRow(0.25);
		addItem(new ReturnButton(observer,layout.getButtonLayout(), parent));
		addRow();
		
		for (int i = 0; i < list.size()/3; i++) {
			for (int j = 0; j < 3; j++) {
				int index = 3*i + j;
				if (index >= list.size())
					break;
				else
					addItem(new EditButton(layout.getButtonLayout(),list.get(index)));
			}
			addRow(0.25);
		}
		
		overlay = new MOverlay(layout.getButtonLayout(),"Press any key to continue");
		addItem(overlay);
		overlay.setVisible(false);
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void onEnter(GameMenu menu) {
		input.addListener(listener);
	}

	@Override public void onLeave(GameMenu menu) {
		input.stopBindings();
		input.removeListener(listener);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Converts a button to a string. **/
	private String buttonToString(Button b) {
		return b.name().replaceAll("_", " ").toUpperCase();
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class EditButton extends MButton {

		private final Button button;
		
		public EditButton(ButtonLayout layout, Button button) {
			super(	getImageObserver(), layout, false, 
					buttonToString(button) + ": " + 
					input.getButtonInfo(button).getName());
			this.button = button;
		}

		@Override public void onClick(GameMenu menu) {
			if (input.startBinding(button)) {
				overlay.setVisible(true);
				editableButton = this;
			}
		}
		
	}
	
	private final class InputListenerImpl implements InputListener {

		@Override public void primaryAxisChanged(Input input, double direction, double value) {}

		@Override public void secondaryAxisChanged(Input input, double direction, double value) {}

		@Override public void buttonPressed(Input input, Button button) {}

		@Override public void buttonReleased(Input input, Button button) {}

		@Override public void bindingAccepted(Input input, Button button) {
			overlay.setVisible(false);
			editableButton.setText(	buttonToString(button) + ": " + 
									input.getButtonInfo(button).getName());
		}
		
	}

}
